<?php

declare(strict_types=1);

namespace app\model\member;

use owns\interface\OwnsModelInteface;

class MemberModel extends OwnsModelInteface
{
    protected $name = 'member';

    protected $field = [];

    /**
     * 查询条件
     * @param object $query
     * @param array $param
     */
    public function scopeCustom(object $query, array $param)
    {
        // ID
        if ($id = $param['id'] ?? 0) {
            $query->where('id', $id);
        }
        // 状态 (0正常 1停用)
        $status = $param['status'] ?? '';
        if (is_numeric($status)) {
            $query->where('status', $status);
        }
        // 区域认证: 0未认证 1已认证
        $regionAttestation = $param['region_attestation'] ?? '';
        if (is_numeric($regionAttestation)) {
            $query->where('region_attestation', $regionAttestation);
        }
        // 昵称
        if ($nickname = $param['nickname'] ?? '') {
            $query->whereLike('nickname', trim($nickname) . '%');
        }
        // 手机号
        if ($mobile = $param['mobile'] ?? '') {
            $query->whereLike('mobile', trim($mobile) . '%');
        }
        // 姓名
        if ($name = $param['name'] ?? '') {
            $query->whereLike('name', trim($name) . '%');
        }
    }

    /**
     * 会员详情
     */
    public function info()
    {
        return $this->hasOne(MemberInfoModel::class, 'member_id', 'id');
    }
}
